package com.sls.web.manager.action.game;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.Tencent;
import com.sls.web.manager.game.pojo.tencent.TencentLoginInfo;
import com.sls.web.manager.game.querybean.TencentQuery;
import com.sls.web.manager.game.service.TencentService;
import com.sls.web.manager.page.Pagination;

public class TencentAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	@Resource(name = "tencentService")
	private TencentService tencentService;
	private TencentQuery tencentQuery;
	private Pagination<Tencent> page;
	private Tencent tencent;
	private String goodsId;
	private int priceRatio;
	private String supplierId;
	private String orderId;
	private String tran_date;

	// =======================================================================测试
	private String loginAccount;
	private String passwordKey;
	private String extraCode;
	private String fileName;
	private String loginResult;

	/**
	 * 获取验证码
	 */
	public String obtainExtraCode() throws Exception {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			@SuppressWarnings("deprecation")
			String path = request.getRealPath("/");
			String now = null;
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			now = format.format(new Date());
			fileName = now + ".jpg";
			tencentService.saveExtraCode(loginAccount, path, fileName,
					getCurrentOperator());
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return list();
	}

	/**
	 * 腾讯登录
	 * 
	 * @return
	 * @throws Exception
	 */
	public String testLogin() throws Exception {
		try {
			log.info("腾讯登录加密串是：" + passwordKey);
			log.info("extraCode：" + extraCode);
			TencentLoginInfo ten = tencentService.loginTencent(loginAccount, extraCode, passwordKey,
					getCurrentOperator());
			if (ten != null) {
				String code = "-1";
				code = ten.getLoginResult();
				if ("0000".equals(code)) {
					loginResult = "登录成功";
				}else if ("0001".equals(code)) {
					loginResult = "验证码错误";
				}else if ("0002".equals(code)){
					loginResult = "密码错误";
				}else if ("0003".equals(code)){
					loginResult = "账号错误";
				}
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return list();
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (tencentQuery == null) {
				tencentQuery = new TencentQuery();
				tencentQuery.setPageSize(Constants.INIT_PAGESIZE);
				tencentQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			String productName = tencentQuery.getProductName();
			String goodsId = tencentQuery.getGoodsId();
			productName = FilterStringSpaces.checkStringHasSpace(productName);
			goodsId = FilterStringSpaces.checkStringHasSpace(goodsId);
			tencentQuery.setProductName(productName);
			tencentQuery.setGoodsId(goodsId);
			page = tencentService.queryTencent(tencentQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	public String add() throws Exception {
		return INPUT;
	}

	/**
	 * 腾讯商品添加
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			tencentService.addTencent(tencent, getCurrentOperator());
			redirectionUrl = "tencent!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		try {
			tencent = tencentService.queryTencentByGoodsId(goodsId,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return INPUT;
	}

	public String edit() throws Exception {
		try {
			this.tencentService.updateTencent(tencent, getCurrentOperator());
			redirectionUrl = "tencent!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 腾讯商品审核
	 * 
	 * @return
	 * @throws Exception
	 */
	public String audit() throws Exception {
		try {
			this.tencentService.confirmAudit(supplierId, priceRatio,
					getCurrentOperator());
			redirectionUrl = "tencent!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 订单查询页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toOrder() throws Exception {
		return "order_list";
	}

	/**
	 * 订单查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String orderList() throws Exception {
		int index = tran_date.indexOf("-");
		Date date = null;
		if (index != -1) { // 需要格式转化
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			date = sdf.parse(tran_date);
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
			tran_date = sdf2.format(date);
		}
		orderId = FilterStringSpaces.checkStringHasSpace(orderId);
		Tencent ten = new Tencent();
		ten.setTran_seq(orderId);
		ten.setTran_date(tran_date);
		try {
			tencent = this.tencentService.queryOrder(ten, getCurrentOperator());
			return "order_list";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 订单查询，页面跳转
	 * 
	 * @return
	 */
	public String toOrderWeb() {
		return "order_list";
	}

	public TencentService getTencentService() {
		return tencentService;
	}

	public void setTencentService(TencentService tencentService) {
		this.tencentService = tencentService;
	}

	public TencentQuery getTencentQuery() {
		return tencentQuery;
	}

	public void setTencentQuery(TencentQuery tencentQuery) {
		this.tencentQuery = tencentQuery;
	}

	public Pagination<Tencent> getPage() {
		return page;
	}

	public void setPage(Pagination<Tencent> page) {
		this.page = page;
	}

	public Tencent getTencent() {
		return tencent;
	}

	public void setTencent(Tencent tencent) {
		this.tencent = tencent;
	}

	public String getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public int getPriceRatio() {
		return priceRatio;
	}

	public void setPriceRatio(int priceRatio) {
		this.priceRatio = priceRatio;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getTran_date() {
		return tran_date;
	}

	public void setTran_date(String tran_date) {
		this.tran_date = tran_date;
	}

	public String getLoginAccount() {
		return loginAccount;
	}

	public void setLoginAccount(String loginAccount) {
		this.loginAccount = loginAccount;
	}

	public String getPasswordKey() {
		return passwordKey;
	}

	public void setPasswordKey(String passwordKey) {
		this.passwordKey = passwordKey;
	}

	public String getExtraCode() {
		return extraCode;
	}

	public void setExtraCode(String extraCode) {
		this.extraCode = extraCode;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getLoginResult() {
		return loginResult;
	}

	public void setLoginResult(String loginResult) {
		this.loginResult = loginResult;
	}

}
